var express = require("express");
var router = express.Router();
var pool = require('../config/mysqlconfig');
// http://localhost:3000/login/signin
router.post('/signin',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            let username = req.body.username;
            let password = req.body.password;
            let data = [username,password];
            let sql = "select * from admin where username=? and password=?";
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})

router.post('/signup',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            let username = req.body.username;
            let password = req.body.password;
            let email = req.body.email;
            let data = {
                username:username,
                password:password,
                email:email
            };
            let sql = "insert into admin set ?";
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})

router.get('/all',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("err")
        }else{
            let sql = "select * from admin";
            conn.query(sql,function(error,results){
                if(error){
                    console.log(error)
                }
                res.send(results)
                conn.release()
            })
        }
    })
})


module.exports = router;